<?php

namespace App\Http\Controllers\Finance;

use App\Common\Traits\Model\ModelSearchTrait;
use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Finance\FinanceCustomerBill;
use App\Models\Finance\FinanceShopSubAccountBill;
use Illuminate\Http\Request;
use App\Jobs\Finance\FinanceBillJob;

class FinanceBillController extends Controller
{
    use ResponseData,ModelSearchTrait;

    protected string $customerColumns='id,company_name,bill_settlement_group,advance_order';
    protected string $branchColumns='id,customer_bill_id,department_id,department_name,bill_imgs';

    //
    public function billList(Request $request, FinanceCustomerBill $customerBill){
        FinanceBillJob::dispatch();
        $customerBill=$this->singleModelSearch($request,$customerBill,$this->listCondition());
        $bills=$customerBill->with($this->listWith())->orderByDesc('id')->paginate($request->size??20);

        return $this->success($bills);
    }
    //
    public function listCondition(){
        return [
            ['bill_status','='],
            ['bill_date','='],
        ];
    }
    //
    public function listWith(){
        return [
            'customer',
        ];
    }
    //
    public function detailWith(){
        return [
            'accountBills',
            'customer',
            'branchBills',
        ];
    }

    //客户账单详情
    public function billShow(Request $request){
        $bill=FinanceCustomerBill::with($this->detailWith())
            ->find($request->bill_id??0);
        if(empty($bill)){
            return $this->failed('id错误，无此账单',201);
        }
        return $this->success($bill);
    }

    //更新客户总账单
    public function updateCustomerBill(Request $request){
        $bill=FinanceCustomerBill::find($request->bill_id??0);
        if(empty($bill)){
            return $this->failed('id错误，无此账单',201);
        }
        $data=$request->except('bill_id');
        $bill->update($data);
        return $this->success('更新成功');
    }

    //更新账单子账号详细信息
    public function updateShopSubAccountBill(Request $request){
        $accountBill=FinanceShopSubAccountBill::find($request->account_bill_id??0);
        if(empty($accountBill)){
            return $this->failed('id错误，无此账单',201);
        }
        $data=$request->except('account_bill_id');

//        $data=$this->getTotalReceive($data);

        $accountBill->update($data);

        $this->updateBillTotal($accountBill);

        return $this->success('更新成功');
    }
    //计算回款项总和
    public function getTotalReceive($data){
        $data['all_received']=($data['pdd_received']??0)
            +($data['previous_reserved']??0)
            +($data['deposit_cost']??0)
            +($data['other_received']??0);
        return $data;
    }

    //统计总账单数据
    public function updateBillTotal($accountBill){
        $totalData=$this->calcData($accountBill);
        return $accountBill->bill()->update($totalData);
    }
    //获取总账单数据
    public function calcData($accountBill){
        return [
            'total_service_days'=>$this->sumColumnData($accountBill->bill_id,'service_days'),
            'total_consult'=>$this->sumColumnData($accountBill->bill_id,'consult_quantity'),
//            'day_average_consult'=>$this->sumColumnData($accountBill->bill_id,'service_days'),
//            'this_period_total'=>$this->sumColumnData($accountBill->bill_id,'all_this_period_fee'),
//            'previous_period_receivable'=>$this->sumColumnData($accountBill->bill_id,'previous_receivable'),
//            'received_total'=>$this->sumColumnData($accountBill->bill_id,'all_received'),
//            'total_customer_pay'=>$this->sumColumnData($accountBill->bill_id,'customer_period_pay'),
        ];
    }
    //计算各子账号数据总和
    public function sumColumnData($billId,$column){
        return FinanceShopSubAccountBill::where('bill_id',$billId)->sum($column);
    }


}
